<section class="page-hd">
    <header>
        <p class="title-description title-top">
            添加账号
        </p>
    </header>
    <hr>
</section>
<form method="post" id="userForm">
    <input type="hidden" name="userId" value="{$model.userId}">
    <div class="form-group-col-2">
        <div class="form-label">用户名称：</div>
        <div class="col-sm-5">
            {empty name="$model"}
            <input type="text" name="userName" placeholder="用户名称..." class="form-control form-boxed form-width">
            {else/}
            <span style="line-height: 35px;">{$model.userName}</span>
            <input type="hidden" name="userName" value="{$model.userName}">
            {/empty}
        </div>
    </div>

    <div class="form-group-col-2">
        <div class="form-label">所属权限：</div>
        <div class="col-sm-5">
            <select style="width:auto;" name="grade">
                <option value="">请选择权限</option>
                <option value="0" {eq name="$model.grade" value="0" }selected{/eq}>超级管理员</option>
                <option value="1" {eq name="$model.grade" value="1" }selected{/eq}>普通用户</option>
            </select>
        </div>
    </div>

    <div class="form-group-col-2">
        <div class="form-label">用户密码：</div>
        <div class="col-sm-5">
            <input type="password" name="userPwd" placeholder="用户密码..." class="form-control form-boxed form-width">
        </div>
    </div>

    <div class="form-group-col-2">
        <div class="form-label">确认用户密码：</div>
        <div class="col-sm-5">
            <input type="password" name="userPwd1" placeholder="确认用户密码..." class="form-control form-boxed form-width">
        </div>
    </div>

    <div class="form-group-col-2">
        <div class="form-label">状态：</div>
        <div class="col-sm-5">
            <label class="check-box">
                {if condition="$model.state == 0 || empty($model)"}
                <input type="checkbox" name="state" checked/>
                {else/}
                <input type="checkbox" name="state"/>
                {/if}
                <span>允许登录</span>
            </label>
        </div>
    </div>

    <div class="form-group-col-2">
        <div class="form-label"></div>
        <div class="form-cont">
            <input type="button" id="userSubmit" class="btn btn-primary" value="添加用户" data-url="{empty name='$model'}{:url('user/add')}{else/}{:url('user/detail')}{/empty}"/>
        </div>
    </div>
</form>

<script>
    $('#userSubmit').on('click', function () {
        var url = $(this).attr('data-url');
        Ajax(url, $('#userForm').serialize());
    });
    function Ajax(url, data) {
        $.ajax({
            url: url,
            cache: false,
            dataType: 'json',
            data: data,
            type: 'post',
        }).done(function (res) {
            if (res.code == 200) {
                layer.alert(res.msg, function () {
                    location.href = '{:url("user/index")}';
                });
            } else {
                layer.alert(res.msg);
            }
        }).fail(function (e) {
            layer.alert(e);
        });
    }
</script>